package com.kaiwukj.android.ufamily;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.app.MyApplication;
import com.kaiwukj.android.ufamily.mvp.helper.TokenStore;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.UserInfoParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.LoginResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.UserResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.model.AccountModel;
import com.kaiwukj.android.ufamily.utils.AESEncrypt;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@RunWith(AndroidJUnit4.class)
public class AccountTest {

    // @Inject
    AccountModel mModel;
    //RxErrorHandler mErrorHandler;

    @Before
    public void init() {

        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();


        AppComponent appComponent = MyApplication.getInstance().getAppComponent();
        mModel = new AccountModel(appComponent.repositoryManager());

        /*mErrorHandler = RxErrorHandler.builder()
                .with(appContext)
                .responseErrorListener(new ResponseErrorListenerImpl())
                .build();*/
    }

    @Test
    public void testSendVerifyCode() {
        mModel.sendVerifyCode("15949581550")
                .compose(RxUtils.schedulerHelper())
                .doOnSubscribe(s -> TokenStore.clear())
                .subscribe(new ApiCallBack<Integer>() {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.d("result----------------->" + GsonUtils.toJson(result));
                    }

                    @Override
                    public void onFailure(@NonNull Throwable e) {
                        super.onFailure(e);
                        LogUtils.e("e------------------------->" + e.getLocalizedMessage());
                    }
                });
    }

    @Test
    public void testLoginByPass() {
        TokenStore.clear();
        mModel.loginByPass("17770821515", AESEncrypt.encrypt("111111"))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<LoginResult>() {
                    @Override
                    public void onSuccess(@NonNull LoginResult result) {
                        TokenStore.setToken(result.getAccess_token(), result.getToken_type());
                        LogUtils.d("result----------------->" + GsonUtils.toJson(result));
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                        LogUtils.e("e------------------------->" + e.getLocalizedMessage());
                    }
                });
    }

    @Test
    public void testLoginByCode() {
        mModel.loginByCode("1549581550", "1234")
                .doOnSubscribe(s -> TokenStore.clear())
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<LoginResult>() {
                    @Override
                    public void onSuccess(@NonNull LoginResult result) {
                        TokenStore.setToken(result.getAccess_token(), result.getToken_type());
                        LogUtils.d("result----------------->" + GsonUtils.toJson(result));
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                        LogUtils.e("e------------------------->" + e.getLocalizedMessage());
                    }
                });
    }

    @Test
    public void testUpdatePwd() throws UnsupportedEncodingException {
        mModel.updatePass(URLEncoder.encode(AESEncrypt.encrypt("1111111111111q"),"utf-8"))
                .doOnSubscribe(s -> TokenStore.setToken("36cd32e9-86ac-4a6a-a707-c52493064a49"))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<Integer>() {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.d("result-------------------->" + result);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        LogUtils.e("e------------------------->" + e.getLocalizedMessage());
                    }
                });
    }

    @Test
    public void testGetUserInfo() {
        //LogUtils.d("token-------------------->" + TokenStore.getToken());
        TokenStore.setToken("2897596c-1a58-47bb-8957-629889431dee", "bearer");
        LogUtils.d("token-------------------->" + TokenStore.getToken());
        mModel.getUserInfo()
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<UserResult>() {
                    @Override
                    public void onSuccess(@NonNull UserResult result) {
                        LogUtils.d("result----------------->" + GsonUtils.toJson(result));
                    }
                });
    }

    @Test
    public void testUpdateUserInfo() {
        UserInfoParams params = new UserInfoParams();
        params.setNickName("守夜人");
        mModel.updateUserInfo(params)
                .subscribe(new ApiCallBack<Integer>() {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        LogUtils.d("result----------------->" + result);
                    }
                });
    }

    @Test
    public void testLogout() {
        TokenStore.clear();
    }


    @Test
    public void testBase64() {

        String data = "123456";
        System.out.println("---------------->" + AESEncrypt.encrypt("123456"));
        //System.out.println("pass---------------->" + new String(Base64.encode(EncryptUtils.encryptAES(data.getBytes(), key.getBytes(), transform, key.getBytes()), Base64.DEFAULT)));
    }
}
